//
//  BasePageViewController.m
//  OSChina
//
//  Created by 8b410-2 on 16/12/25.
//  Copyright © 2016年 abb. All rights reserved.
//

#import "OSCBasePageViewController.h"
#import "OSCPageViewModel.h"
#import "OSCRouter.h"
#import "UIView+Blank.h"
#include "OSCTweetTableViewController.h"
@interface OSCBasePageViewController () <UIScrollViewDelegate>
@property (nonatomic, strong, readonly) OSCPageViewModel *viewModel;
@property (nonatomic, copy) NSArray *controllers;
@end

@implementation OSCBasePageViewController

@dynamic viewModel;

- (instancetype)initWithViewModel:(OSCBaseViewModel *)viewModel {
    self = [super initWithViewModel:viewModel];
    if (self) {
        // 根据viewModel里的viewModels初始化子控制器,并添加到子控制器数组里
        OSCRouter *router = [OSCRouter sharedInstance];// viewModel-viewController映射路由表
        NSMutableArray *controllersArr = [NSMutableArray new];
        for (OSCBaseViewModel *viewModel in self.viewModel.viewModels) {
            [controllersArr addObject:[router viewControllerForViewModel:viewModel]];
            [self addChildViewController:[router viewControllerForViewModel:viewModel]];
        }
        self.controllers = [controllersArr copy];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setSelf];
    [self addContentView];
}

- (void)bindViewModel {
    [super bindViewModel];
    // 创建快照信号 给动弹列表的selectedTuple添加观察 并合并到快照信号上
    RACSignal *snapSignal = [RACSignal empty];
    for (UIViewController *vc in self.controllers) {
        if ([vc isKindOfClass:[OSCTweetTableViewController class]]) {
            OSCTweetTableViewController *tweetVC = (OSCTweetTableViewController *)vc;
            snapSignal = [snapSignal merge:RACObserve(tweetVC, selectedTuple)];
        }
    }
    // 将快照元组绑定在快照信号上
    RAC(self,snapshotTuple) = snapSignal;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    // 判断是否需要<提醒登录页>
    if (![OSCAccountTools isLogin]) {
        self.scrollView.hidden = YES;
        [self.view showLoginPageViewWithRect:self.scrollView.frame];
        @weakify(self);
        self.view.loginPageView.didClickLogInBlock = ^() {// 点击登陆按钮
            @strongify(self);
            [self.viewModel presentLoginViewModel];
        };
    } else {
        self.scrollView.hidden = NO;
        [self.view hideloginPageView];
        // 手动触发子控制器的viewDidAppear
        for (UIViewController *vc in self.controllers) {
            [vc viewDidAppear:YES];
        }
    }
}

- (void)setSelf {
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)addContentView{
    // 标题bar
    TitleBarView *titleBar = [[TitleBarView alloc] initWithFrame:CGRectMake(0, 64, kScreen_Width, 36) andTitles:self.viewModel.titles];
    titleBar.backgroundColor = [UIColor titleBarColor];
    titleBar.titleButtonClicked = ^(NSUInteger index){
        [_scrollView setContentOffset:CGPointMake(kScreen_Width * index, 0) animated:YES];
    };
    _titleBar = titleBar;
    [self.view addSubview:_titleBar];
    // scrollView
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_titleBar.frame), kScreen_Width, kScreen_Height - CGRectGetMaxY(_titleBar.frame) - 49)];
    scrollView.contentSize = CGSizeMake(kScreen_Width * self.controllers.count, 0);
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView = scrollView;
    [self.view addSubview:_scrollView];
    
    for (UIViewController *childVC in self.controllers) {
        NSInteger index = [self.controllers indexOfObject:childVC];
        childVC.view.frame = CGRectMake(kScreen_Width * index, 0, kScreen_Width, CGRectGetHeight(_scrollView.frame));
        [_scrollView addSubview:childVC.view];
    }
}

#pragma mark --- UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger index = scrollView.contentOffset.x / kScreen_Width;
    [self.titleBar scrollToCenterWithIndex:index];
}

@end
